package m2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        // 5672 端口，客户端应收发消息的端口
        // 15672 端口。管理控制台界面的访问端口
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();  //通信的通道
        //在服务器上创建队列：helloworld
        //如果队列已经存在，不回重复的创建
        c.queueDeclare("task_queue",true,false,false,null);
        //在控制台输入，向 helloworld 队列发送消息
        while (true){
            System.out.println("输入消息：");
            String s = new Scanner(System.in).nextLine();
            c.basicPublish("", "task_queue", MessageProperties.PERSISTENT_BASIC, s.getBytes());
        }
    }
}
